﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using StarTrek.Ship;
using Weapons = System.Collections.Generic.List<StarTrek.Weapon.iWeapon>;

namespace StarTrek.Weapon
{
    public class WeaponList:ICollection<iWeapon>
    {
        private Weapons cWeapon = new Weapons();
        private iShip cParent = null;

        public iWeapon GetWeaponType(string weapon)
        {
            return cWeapon.Find(
                (iWeapon w) => w.GetType().Equals(System.Reflection.Assembly.GetExecutingAssembly().GetType("StarTrek.Weapon." + weapon))
                );
        }
        public iWeapon GetWeaponName(string weapon)
        {
            return cWeapon.Find(
                (iWeapon w) => w.Name.Equals(weapon)
                );
        }
        internal static WeaponList LoadWeapon(XmlNode given, BaseShip Parent)
        {
            WeaponList rVal = new WeaponList(Parent);
            const string WEAPON_NAMESPACE = "StarTrek.Weapon.";
            foreach (XmlNode item in given.ChildNodes)
            {
                iWeapon wep = System.Reflection.Assembly.GetExecutingAssembly()
                                  .GetType(WEAPON_NAMESPACE + item.Name)
                                  .GetConstructor(Type.EmptyTypes)
                                  .Invoke(new object[0]) as iWeapon;

                if (wep != null)
                {
                    wep.Load(item);
                    rVal.Add(wep);
                }
            }
            Parent.Weapons = rVal;
            return rVal;
        }

        public WeaponList(iShip Parent)
        {
            cParent = Parent;
        }

        internal List<iWeapon> InnerList
        {
            get {return cWeapon; }
        }
        #region ICollection<iWeapon> Members

        public void Add(iWeapon item)
        {
            cWeapon.Add(item);
            item.Parent = cParent;
        }

        public void Clear()
        {
            cWeapon.Clear();
        }

        public bool Contains(iWeapon item)
        {
            return cWeapon.Contains(item);
        }

        public void CopyTo(iWeapon[] array, int arrayIndex)
        {
            cWeapon.CopyTo(array,arrayIndex);
        }

        public int Count
        {
            get { return cWeapon.Count; }
        }

        public bool IsReadOnly
        {
            get { return (cWeapon as ICollection<iWeapon>).IsReadOnly; }
        }

        public bool Remove(iWeapon item)
        {
            item.Parent = null;
            return cWeapon.Remove(item);
        }

        #endregion

        #region IEnumerable Members

        public IEnumerator<iWeapon> GetEnumerator()
        {
            return cWeapon.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return cWeapon.GetEnumerator();
        }

        #endregion
    }
}
